Crate rfc6979

source ·
Expand description

RustCrypto: RFC6979 Deterministic Signatures

crate Docs Build Status Apache2/MIT licensed MSRV Project Chat

Pure Rust implementation of RFC6979: Deterministic Usage of the Digital Signature Algorithm (DSA) and Elliptic Curve Digital Signature Algorithm (ECDSA).

Algorithm described in RFC 6979 § 3.2: https://tools.ietf.org/html/rfc6979#section-3

Documentation

Minimum Supported Rust Version

This crate requires Rust 1.61 at a minimum.

We may change the MSRV in the future, but it will be accompanied by a minor version bump.

License

All crates licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Usage

See also: the documentation for the generate_k function.

use hex_literal::hex;
use rfc6979::consts::U32;
use sha2::{Digest, Sha256};

// NIST P-256 field modulus
const NIST_P256_MODULUS: [u8; 32] =
    hex!("FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551");

// Public key for RFC6979 NIST P256/SHA256 test case
const RFC6979_KEY: [u8; 32] =
    hex!("C9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721");

// Test message for RFC6979 NIST P256/SHA256 test case
const RFC6979_MSG: &[u8; 6] = b"sample";

// Expected K for RFC6979 NIST P256/SHA256 test case
const RFC6979_EXPECTED_K: [u8; 32] =
    hex!("A6E3C57DD01ABE90086538398355DD4C3B17AA873382B0F24D6129493D8AAD60");

let h = Sha256::digest(RFC6979_MSG);
let aad = b"";
let k = rfc6979::generate_k::<Sha256, U32>(&RFC6979_KEY.into(), &NIST_P256_MODULUS.into(), &h, aad);
assert_eq!(k.as_slice(), &RFC6979_EXPECTED_K);

Modules

  • Type aliases for many constants.

Structs

  • Internal implementation of HMAC_DRBG as described in NIST SP800-90A.

Functions

  • Deterministically generate ephemeral scalar k.

Type Definitions

  • Array of bytes representing a scalar serialized as a big endian integer.